package com.cskaoyan.user.controller;

import com.alibaba.fastjson.JSON;
import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.mall.commons.result.TokenInfo;
import com.cskaoyan.mall.commons.util.CookieUtil;
import com.cskaoyan.user.converter.UserConverterMapper;
import com.cskaoyan.user.dto.LoginVerifyResult;
import com.cskaoyan.user.dto.UserLoginResponse;
import com.cskaoyan.user.dto.UserLoginResult;
import com.cskaoyan.user.form.KaptchaCodeRequest;
import com.cskaoyan.user.form.UserLoginRequest;
import com.cskaoyan.user.service.IKaptchaService;
import com.cskaoyan.user.service.ILoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("user")
public class LoginController {

    @Autowired
    private ILoginService loginService;

    @Autowired
    private IKaptchaService kaptchaService;

    @Autowired
    private UserConverterMapper userConverter;

    @PostMapping("login")
    public ResponseData<UserLoginResult> login(@Validated @RequestBody UserLoginRequest loginReq,
           HttpServletRequest request, HttpServletResponse response) {
        // 校验验证码
        String captcha = loginReq.getCaptcha();
        KaptchaCodeRequest codeRequest = new KaptchaCodeRequest();
        String uuid = CookieUtil.getCookieValue(request, "kaptcha_uuid");
        codeRequest.setUuid(uuid);
        codeRequest.setCode(captcha);
        kaptchaService.validateKaptchaCode(codeRequest);
        // 业务处理
        UserLoginResponse loginResponse = loginService.login(loginReq);
        UserLoginResult result = userConverter.loginResp2Result(loginResponse);

        // 存储token到cookie
        Cookie cookie = CookieUtil.genCookie("access_token", result.getToken(), "/",
                60 * 60 * 24);
        cookie.setHttpOnly(true);
        CookieUtil.setCookie(response, cookie);
        return new ResponseUtil<UserLoginResult>().setData(result);
    }

    @GetMapping("login")
    public ResponseData<LoginVerifyResult> verifyLogin(@RequestHeader String user_info) {
        TokenInfo tokenInfo = JSON.parseObject(user_info, TokenInfo.class);
        Long uid = tokenInfo.getUid();
        LoginVerifyResult result = loginService.verify(uid);
        return new ResponseUtil<LoginVerifyResult>().setData(result);
    }

    @GetMapping("loginOut")
    public ResponseData<Object> logout(HttpServletRequest request, HttpServletResponse response) {
        // 退出只需清除cookie
        String accessToken = CookieUtil.getCookieValue(request, "access_token");
        Cookie cookie = CookieUtil.genCookie("access_token", accessToken, "/", 0);
        cookie.setHttpOnly(true);
        response.addCookie(cookie);
        return new ResponseUtil<>().setData(null);
    }
}
